<?php
namespace Admin\Controller;
use Think\Controller;

class SystemController extends CommonController
{
    //如果未登录，就跳转到登录页面
    public function _initialize()
    {
        $params = array();
        parent::_initialize($params);
        $this->assign("now_module" ,"conf");
    }

    // 网站信息
    public function site()
    {
        // 邮件发送已成功配置
        if(IS_POST)
        {
            $sitename = trim(I('post.sitename'));
            $whyclose = trim(I('post.whyclose'));
            $siteclose = to_int(I('post.siteclose'));
            $sitetitle = trim(I('post.sitetitle'));
            $siteaddress = trim(I('post.siteaddress'));
            $sitedesc = trim(I('post.sitedesc'));
            $sitelogo = trim(I('post.sitelogo'));
            $login_type = I('post.login_type');
            if (empty($sitename)) {
                $this->error('请填写网站名称!');
            }

            if (empty($sitetitle)) {
                $this->error('请填写网站标题!');
            }

            if (empty($sitedesc)) {
                $this->error('请填写网站描述!');
            }

            if (empty($siteaddress)) {
                $this->error('请填写所在地区!');
            }


            M("config")->where("conf_type = 'system' AND conf_key = 'sitelogo'")->setField("conf_value" ,$sitelogo);
            M("config")->where("conf_type = 'system' AND conf_key = 'login_type'")->setField("conf_value" ,$login_type);
            M("config")->where("conf_type = 'system' AND conf_key = 'sitename'")->setField("conf_value" ,$sitename);
            M("config")->where("conf_type = 'system' AND conf_key = 'whyclose'")->setField("conf_value" ,$whyclose);
            M("config")->where("conf_type = 'system' AND conf_key = 'siteclose'")->setField("conf_value" ,$siteclose);
            M("config")->where("conf_type = 'system' AND conf_key = 'sitetitle'")->setField("conf_value" ,$sitetitle);
            M("config")->where("conf_type = 'system' AND conf_key = 'siteaddress'")->setField("conf_value" ,$siteaddress);
            M("config")->where("conf_type = 'system' AND conf_key = 'sitedesc'")->setField("conf_value" ,$sitedesc);
            $this->success("修改成功" ,U("System/site") ,1);
        }else{
            $conf = M("config")->where("conf_type = 'system'")->select();
            foreach($conf as $v){
                $confs[$v['conf_key']] = $v['conf_value'];
            }
            $this->assign("configs" ,$confs);
            $this->display();
        }
    }

    // 基本配置
    public function information()
    {
        // 邮件发送已成功配置
        if(IS_POST)
        {
            $stock_warning = to_int(I('post.stock_warning'));
            $agent_check = to_int(I('post.agent_check'));
            $over_order_time = to_int(I('post.over_order_time'));
            $module_msg = trim(I('post.module_msg'));
            $hot_search = trim(I('post.hot_search'));
            $over_day = to_int(I('post.over_day'));
            $open_code = to_int(I('post.open_code'));
            $order_send_sms = to_int(I('post.order_send_sms'));

            M("config")->where("conf_type = 'shop' AND conf_key = 'order_send_sms'")->setField("conf_value" ,$order_send_sms);
            M("config")->where("conf_type = 'shop' AND conf_key = 'open_code'")->setField("conf_value" ,$open_code);
            M("config")->where("conf_type = 'shop' AND conf_key = 'open_code'")->setField("conf_value" ,$open_code);
            M("config")->where("conf_type = 'shop' AND conf_key = 'module_msg'")->setField("conf_value" ,$module_msg);
            M("config")->where("conf_type = 'shop' AND conf_key = 'stock_warning'")->setField("conf_value" ,$stock_warning);
            M("config")->where("conf_type = 'shop' AND conf_key = 'hot_search'")->setField("conf_value" ,$hot_search);
            M("config")->where("conf_type = 'shop' AND conf_key = 'agent_check'")->setField("conf_value" ,$agent_check);
            M("config")->where("conf_type = 'shop' AND conf_key = 'over_day'")->setField("conf_value" ,$over_day);
            M("config")->where("conf_type = 'shop' AND conf_key = 'over_order_time'")->setField("conf_value" ,$over_order_time);
            $this->success("修改成功" ,U("System/information") ,1);
        }else{
            $conf = M("config")->where("conf_type = 'shop'")->select();
            foreach($conf as $v){
                $confs[$v['conf_key']] = $v['conf_value'];
            }
            $this->assign("configs" ,$confs);
            $this->display();
        }
    }

    // 邮件配置
    public function smtp() {
        // 邮件发送已成功配置
        if(IS_POST) {
            $from_email = trim(I('post.from_email'));
            $smtp = trim(I('post.smtp'));
            $port = trim(I('post.port'));
            $loginname = trim(I('post.loginname'));
            $password = trim(I('post.password'));
            $ssl = to_int(I('post.ssl'));

            if (!isEmail($from_email)) {
                $this->error('请填写正确的邮箱!');
            }
            if (empty($smtp)) {
                $this->error('请填写SMTP服务器!');
            }
            if (empty($port)) {
                $this->error('请填写SMTP端口!');
            }
            if (empty($loginname)) {
                $this->error('请填写登录名!');
            }
            if (empty($password)) {
                $this->error('请填写密码!');
            }

            M("config")->where("conf_key = 'from_email'")->setField("conf_value" ,$from_email);
            M("config")->where("conf_key = 'smtp'")->setField("conf_value" ,$smtp);
            M("config")->where("conf_key = 'port'")->setField("conf_value" ,$port);
            M("config")->where("conf_key = 'loginname'")->setField("conf_value" ,$loginname);
            M("config")->where("conf_key = 'password'")->setField("conf_value" ,$password);
            M("config")->where("conf_key = 'ssl'")->setField("conf_value" ,$ssl);
            $this->success("修改成功" ,U("System/smtp") ,1);
        }else{
            $conf = M("config")->where("conf_type = 'smtp'")->select();
            foreach($conf as $v){
                $confs[$v['conf_key']] = $v['conf_value'];
            }
            $this->assign("configs" ,$confs);
            $this->display();
        }
    }

    // SMS短信接口
    public function sms()
    {
        $conf = M("sms_service")->select();
        $this->assign("sms_services" ,$conf);
        $this->display();
    }

    // 测试发送 检查是否配置成功
    public function sendTest()
    {
        $from_email = trim(I('from_email'));
        $smtp = trim(I('smtp'));
        $port = trim(I('port'));
        $loginname = trim(I('loginname'));
        $password = trim(I('password'));
        $ssl = to_int(I('ssl'));
        $test_email = trim(I('test_email'));
        $this->ajaxReturn(TestSendMail($test_email ,$from_email, $smtp ,$port ,$ssl ,$loginname ,$password));
    }

    // 安装短信服务
    public function install_sms()
    {
        $sms_id = trim(I('id'));
        $rs = M("sms_service")->where("is_open = 1")->find();
        if($rs){
            $this->error("请先卸载其他短信服务" ,U("System/sms") ,2);
        }
        M("sms_service")->where("sms_id = %d",array($sms_id))->setField("is_open" ,1);
        $this->success("安装成功" ,U("System/sms") ,1);
    }

    // 卸载短信服务
    public function sms_uninstall()
    {
        $sms_id = trim(I('id'));
        M("sms_service")->where("sms_id = %d",array($sms_id))->setField("is_open" ,2);
        $this->success("卸载成功" ,U("System/sms") ,1);
    }

    // 短信服务编辑
    public function sms_edit()
    {
        if(IS_POST) {
            $accessKeyId = trim(I('post.accessKeyId'));
            $accessKeySecret = trim(I('post.accessKeySecret'));
            $sms_name = trim(I('post.sms_name'));
            $sms_id = to_int(I('post.sms_id'));
            $appkey = trim(I('post.appkey'));
            $secretKey = trim(I('post.secretKey'));

            if (empty($sms_name)) $this->error('请填写服务名称');
            if ($sms_id == 2) {
                if (empty($accessKeyId)) $this->error('请填写accessKeyId');
                if (empty($accessKeySecret)) $this->error('请填写accessKeySecret');
            } else {
                if (empty($appkey)) $this->error('请填写appkey');
                if (empty($secretKey)) $this->error('请填写secretKey');

            }


            $config = I('post.');
            $sms = M("sms_service")->where("sms_id = $sms_id")->find();
            if(!$sms){
                $this->error("请求有误" ,U("System/sms") ,1);
            }
            $sms_conf = array();
            $conf_code = unserialize($sms['sms_params']);
            foreach($conf_code as $conf_k => $conf_v)
            {
                $conf_name = strtolower($conf_v['name']);
                foreach($config as $k => $v){
                    $post_name = strtolower($k);
                    if($post_name == $conf_name){
                        $conf_code[$conf_k]["value"] = $v;
                        $sms_conf[$conf_name] = $v;
                    }
                }
            }
            $save_data["sms_params"] = serialize($conf_code);
            $save_data["sms_name"] = trim(I('post.sms_name'));
            $save_data["sms_conf"] = serialize($sms_conf);
            M("sms_service")->where("sms_id = $sms_id")->save( $save_data);
            $this->success("操作成功" ,U("System/sms") ,1);
        }else{
            $sms_id = to_int(I("get.id"));
            $sms_service = M("sms_service")->where("sms_id = %d",array($sms_id))->find();
            if(!$sms_service){
                $this->error("请求有误" ,U("System/sms") ,1);
            }
            $this->assign("sms_html" ,build_payment_html( $sms_service['sms_params'] ));
            $this->assign("sms_id" ,$sms_id);
            $this->assign("sms" ,$sms_service);
            $this->display();
        }
    }

    // 短信模板
    public function sms_template()
    {
        $tpl_sence = array(
            1 => '注册验证',
            2 => '订单消息',
            3 => '充值消息'
        );
        $sms_templates = M("sms_template t")
            ->join(C("DB_PREFIX")."sms_service s ON s.sms_id = t.sms_id","LEFT")
            ->field("s.sms_name AS sms_sv,t.*")
            ->select();
        $this->assign("sms_templates", $sms_templates);
        $this->assign("tpl_scene", $tpl_sence);
        $this->display();
    }

    // 删除模板
    public function del_sms_template()
    {
        if(IS_POST)
        {
            $tpl_id = to_int(I('post.tpl_id'));
            M("sms_template")->where("tpl_id = $tpl_id")->delete();
            $this->ajaxReturn(array("status" => "success"));
        }else{
            exit("error");
        }
    }

    //添加模板
    public function add_sms_template()
    {
        if(IS_POST)
        {
            $sms_id = to_int(I('post.sms_id'));
            $send_scene = to_int(I('post.send_scene'));
            $isset = M("sms_template")->where("sms_id = %d AND send_scene = %d" ,array($sms_id, $send_scene))->find();
            if($isset){
                $this->error("此场景模板已存在" ,U("System/add_sms_template") ,1);
            }
            M("sms_template")->add(array(
                "sms_id" => $sms_id,
                "sms_sign" => trim(I("post.sms_sign")),
                "sms_tpl_code" => trim(I("post.sms_tpl_code")),
                "tpl_content" => trim(I("post.tpl_content")),
                "send_scene" => $send_scene,
                "sms_title" => trim(I("post.sms_title"))
            ));
            $this->success("操作成功" ,U("System/sms_template") ,1);
        }else{
            $tpl_sence = array(1 => '注册验证', 2 => '订单消息', 3 => '充值消息');
            $sms_services = M("sms_service")->field("sms_name,sms_id")->select();
            $this->assign("sms_service", $sms_services);
            $this->assign("tpl_scene", $tpl_sence);
            $this->display();
        }
    }

    //更换主题
    public function theme() {
        if (IS_POST) {
            $root_path = './Application/Common/Conf/theme_conf.php';
            $template = trim(I('post.template'));
            $css = trim(I('post.css'));
            $css_arr = explode('/', $css);
            $end_css = 'css/'.$css_arr[1];
            $data = "<?php return array(
                'NOW_THEME' => '$template',
                'css'=>'$end_css'
            );";
            file_put_contents($root_path,$data);
        } else {
            //获取可用模板
            $templates = array();
            $root_path = './Application/Home/View/';
            $template_dir = @opendir($root_path);
            //可用模板
            while (($file = readdir($template_dir)) !== false) {
                if ($file!='.' && $file!='..' ) {
                    $templates[] = $file;
                }
            }

            $css = array();
            //读取模板下的样式
            foreach ($templates as $v) {
                if ($dh = opendir($root_path.$v.'/Static/css')) {
                    while (($x = readdir($dh)) !== false) {
                        if ($x!='.' && $x!='..') {
                            $css[$v][] = $v.'/'.$x;
                        }
                    }
                }
            }
            @closedir($template_dir);
            $this->assign('css', $css);
            $this->assign('theme', C('NOW_THEME'));
            $this->assign('templates', $templates);
            $this->display();
        }
    }






















}